package com.fyh.controller;

import com.fyh.entity.common.Result;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.entity.job.JobPosition;
import com.fyh.entity.job.JobPositionRequest;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.JobFavorite;
import com.fyh.entity.user.SimpleCandidate;
import com.fyh.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/job")
@Slf4j
public class JobController {

    @Autowired
    JobService jobService;

    /**
     * 获取所有标签
     * @return
     */
    @GetMapping("/tagList")
    public Result<Map<String, List<String>>> getTagList(){
        log.info("获取所有标签");
        Map<String, List<String>> tagList = jobService.getTagList();
        return Result.success(tagList);
    }

    /**
     * 通过EmployerId获取职位列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<SimpleJobPosition>> getSimpleJobListByEmployerId(@RequestParam("employerId") Long employerId){
        log.info("招聘者Id{} 获取职位列表", employerId);

        List<SimpleJobPosition> simpleJobList = jobService.getSimpleJobListByEmployerId(employerId);

        return Result.success(simpleJobList);
    }

    /**
     * 通过职位名称、类型、标签、地址获取职位列表
     * @return
     */
    @PostMapping("/jobList")
    public Result<List<SimpleJobPosition>> getSimpleJobListByNameAndType(@RequestBody JobPositionRequest jobPositionRequest){
        log.info("获取职位列表{}", jobPositionRequest);

        List<SimpleJobPosition> simpleJobList = jobService.getSimpleJobListByNameAndType(jobPositionRequest);

        return Result.success(simpleJobList);
    }

    /**
     * 获取收藏的职位列表
     * @return
     */
    @PostMapping("/getSimpleJobListByPositionId")
    public Result<List<SimpleJobPosition>> getSimpleJobListByPositionId(@RequestBody List<JobFavorite> jobFavoriteList){
        log.info("获取收藏的职位列表", jobFavoriteList);
        List<SimpleJobPosition> simpleJobList = jobService.getSimpleJobListByPositionId(jobFavoriteList);
        return Result.success(simpleJobList);
    }

    /**
     * 通过职位Id获取标签列表
     * @return
     */
    @GetMapping("/tagListByPositionId")
    public Result<List<String>> getTagNameListByPositionId(@RequestParam("positionId") Long positionId){
        log.info("职位Id{} 获取标签列表", positionId);

        List<String> tagList = jobService.getTagNameListByPositionId(positionId);
        log.info("获取标签列表:{}", tagList);
        return Result.success(tagList);
    }

    /**
     * 通过职位Id获取职位详情
     * @return
     */
    @GetMapping("/detail")
    public Result<JobPosition> getJobPositionDetail(@RequestParam("positionId") Long positionId){
        log.info("职位Id{} 获取职位详情", positionId);
        JobPosition jobPosition = jobService.getJobPositionDetail(positionId);
        return Result.success(jobPosition);
    }

    /**
     * 更新职位
     * @return
     */
    @PostMapping("/update")
    public Result updateJobPosition(@RequestBody JobPosition jobPosition){
        log.info("更新职位:{}", jobPosition);
        jobService.updateJobPosition(jobPosition);
        return Result.success();
    }

    /**
     * 添加职位
     * @return
     */
    @PostMapping("/insert")
    public Result addJobPosition(@RequestBody JobPosition jobPosition){
        log.info("添加职位:{}", jobPosition);
        jobService.addJobPosition(jobPosition);
        return Result.success();
    }

    /**
     * 删除职位
     * @return
     */
    @DeleteMapping("/deleteJobPosition")
    public Result deleteJobPosition(@RequestParam("positionId") Long positionId){
        log.info("职位Id{} 删除职位", positionId);
        jobService.deleteJobPosition(positionId);
        return Result.success();
    }

    /**
     * 更新职位状态
     * @return
     */
    @PostMapping("/updateJobStatus/{positionId}")
    public Result updateJobStatus(@RequestBody Integer status, @PathVariable("positionId") Long positionId){
        log.info("职位Id{} 更新职位状态{}", positionId, status);
        jobService.updateJobStatus(status, positionId);
        return Result.success();
    }

    /**
     * 获取职位是否投递
     * @return
     */
    @GetMapping("/getDeliveryExist")
    public Result<Boolean> getDeliveryExist(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        Boolean deliveryExist = jobService.getDeliveryExist(positionId, userId);
        return Result.success(deliveryExist);
    }

    /**
     * 职位投递简历
     * @return
     */
    @PutMapping("/deliveryVitae")
    public Result deliveryVitae(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        log.info("用户{}向职位{}投递简历", userId, positionId);
        jobService.deliveryVitae(positionId, userId);
        return Result.success();
    }

    /**
     * 获取职位求职者ID，投递时间和投递状态列表
     * @return
     */
    @GetMapping("/getSimpleDeliveryRecordListList")
    public Result<List<DeliveryRecord>> getSimpleDeliveryRecordListList(@RequestParam("positionId") Long positionId, @RequestParam("status") String status){
        log.info("获取职位{}下的所有求职者ID列表", positionId);
        List<DeliveryRecord> deliveryRecordList = jobService.getSimpleDeliveryRecordListList(positionId, status);
        return Result.success(deliveryRecordList);
    }

    /**
     * 更新职位求职者投递状态
     * @return
     */
    @PostMapping("/updateDeliveryStatus")
    public Result updateDeliveryStatus(@RequestBody DeliveryRecord deliveryRecord){
        log.info("更新职位{}下的求职者{}投递状态{}", deliveryRecord.getPositionId(), deliveryRecord.getUserId(), deliveryRecord.getStatus());
        jobService.updateDeliveryStatus(deliveryRecord);
        return Result.success();
    }

    /**
     * 通过职位Id获取职位所属招聘者Id
     * @return
     */
    @GetMapping("/getEmployerIdByPositionId")
    public Result<Long> getEmployerIdByPositionId(@RequestParam("positionId") Long positionId){
        log.info("职位Id{} 获取职位所属招聘者Id", positionId);
        Long employerId = jobService.getEmployerIdByPositionId(positionId);
        return Result.success(employerId);
    }
}
